package com.crk.yiyuanguahao.service.impl;



import com.crk.yiyuanguahao.dao.RoleAuthDao;
import com.crk.yiyuanguahao.dao.RoleDao;
import com.crk.yiyuanguahao.dao.UserDao;
import com.crk.yiyuanguahao.entity.*;
import com.crk.yiyuanguahao.service.RoleService;
import com.crk.yiyuanguahao.util.Page;
import com.crk.yiyuanguahao.util.ResponseResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @Author: 程荣凯
 * @Date: 2019/3/4 18:03
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    RoleDao roleDao;
    @Autowired
    UserDao userDao;
    @Autowired
    RoleAuthDao roleAuthDao;
    /**
     * 保存角色
     *
     * @param role
     * @return
     */
    @Override
    public ResponseResult saveRole(Role role) {
        if (null != role){
            int result = 0;
            if (role.getId() == null){
                User user = (User) SecurityUtils.getSubject().getPrincipal();
                role.setCreator(user.getId());
                role.setUpdator(user.getId());
                role.setCreateTime(new Date());
                role.setUpdateTime(new Date());
                role.setStatus((short)1);
                result = roleDao.insert(role);
            }else{
                User user = (User) SecurityUtils.getSubject().getPrincipal();
                role.setUpdator(user.getId());
                role.setUpdateTime(new Date());
                result = roleDao.updateByPrimaryKey(role);
            }

            if (result > 0){
                return ResponseResult.success(role);
            }else{
                return ResponseResult.fail();
            }
        }else {
            return ResponseResult.fail();
        }
    }

    /**
     * 删除角色
     *
     * @param roleId
     * @return
     */
    @Override
    public ResponseResult deleteRoleById(int roleId) {
        int result = roleDao.deleteByPrimaryKey(roleId);
        if (result > 0){
            return ResponseResult.success();
        }else{
            return ResponseResult.fail();
        }
    }

    /**
     * 查询角色列表
     *
     * @param roleCode 角色编码
     * @param roleName 角色名称
     * @param pageInfo 分页信息
     * @return
     */
    @Override
    public ResponseResult getRoleList(String roleCode, String roleName, Page pageInfo) {
        Integer count = roleDao.countByCondition(roleCode, roleName);
        pageInfo.setAllRow(count);
        List<Role> roles = roleDao.selectByCondition(roleCode, roleName, pageInfo.getStart(), pageInfo.getSize());
        return ResponseResult.success(roles);
    }

    /**
     * 查询单个角色信息
     *
     * @param roleId
     * @return
     */
    @Override
    public ResponseResult getRole(int roleId) {
        Role role = roleDao.selectByPrimaryKey(roleId);
        return ResponseResult.success(role);
    }

    /**
     * 更改角色状态
     *
     * @param roleId
     * @param status
     * @return
     */
    @Override
    public ResponseResult changeStatus(int roleId, int status) {
        int result = roleDao.changeStatus(roleId,status);
        if (result > 0){
            return ResponseResult.success();
        }else{
            return ResponseResult.fail();
        }
    }


    private static final String ADMIN = "system_admin";


    /**
     * 根据角色ID查询权限信息
     *
     * @param roleId
     * @return
     */
    @Override
    public List<Function> getFunctionList(Integer roleId) {
        return roleDao.getFunctionList(roleId);
    }

    /**
     * 批量修改用户权限
     *
     * @param roleAuthDto
     * @return
     */
    @Override
    public ResponseResult batchUpdateRoleAuth(RoleAuthDto roleAuthDto) {
        Integer userId = roleAuthDto.getUserId();
        List<Role> roleList = roleAuthDto.getRoleList();
        List<Role> oldRoleList = userDao.getRoleList(userId);
        if (!oldRoleList.isEmpty()){
            roleDao.deleteRoleByUser(userId);
        }
        for (Role role : roleList) {
            RoleAuth roleAuth = new RoleAuth();
            roleAuth.setRoleId(role.getId());
            roleAuth.setPartyId(userId);
            roleAuth.setPartyType((short)1);
            roleAuth.setCreateTime(new Date());
            roleAuth.setUpdateTime(new Date());
            roleAuth.setStatus((short)1);
            User user = (User) SecurityUtils.getSubject().getPrincipal();
            roleAuth.setCreator(user.getId());
            roleAuth.setUpdator(user.getId());
            roleAuthDao.insert(roleAuth);
        }
        return ResponseResult.success();
    }
}
